﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Diploma.Models.IRepositories;

namespace Diploma.Models.Repositories
{
    internal class LoggerRepository : ILoggerRepository
    {
        private readonly CommonEntities dataContext;

        public LoggerRepository(CommonEntities dataContext)
        {
            this.dataContext = dataContext;
        }

        public void AddError(DiplomaError error)
        {
            if (error == null)
                throw new ArgumentNullException();

            error.ErrorID = Guid.NewGuid();
            error.Logger = "Error";
            dataContext.DiplomaErrors.AddObject(error);
            dataContext.SaveChanges();
        }

        public void AddInfo(DiplomaError error)
        {
            if (error == null)
                throw new ArgumentNullException();

            error.ErrorID = Guid.NewGuid();
            error.Logger = "Info";
            dataContext.DiplomaErrors.AddObject(error);
            dataContext.SaveChanges();
        }

        public void AddDebug(DiplomaError error)
        {
            if (error == null)
                throw new ArgumentNullException();

            error.ErrorID = Guid.NewGuid();
            error.Logger = "Debug";
            dataContext.DiplomaErrors.AddObject(error);
            dataContext.SaveChanges();
        }

        public void AddLoginEvent(DiplomaError info)
        {
            if (info == null)
                throw new ArgumentNullException();

            info.ErrorID = Guid.NewGuid();
            info.Logger = "SignIn";
            dataContext.DiplomaErrors.AddObject(info);
            dataContext.SaveChanges();
        }

        public DiplomaError GetError(Guid id)
        {
            return dataContext.DiplomaErrors
                .FirstOrDefault(r => r.ErrorID == id);
        }

        public IEnumerable<DiplomaError> GetAll()
        {
            return dataContext.DiplomaErrors
                .AsParallel()
                .OrderByDescending(r => r.Occured)
                .ToList();
        }

        public IEnumerable<DiplomaError> GetErrors()
        {
            return dataContext.DiplomaErrors
                .AsParallel()
                .Where(r => String.Equals(r.Logger, "Error", StringComparison.InvariantCultureIgnoreCase))
                .OrderByDescending(r => r.Occured)
                .ToList();
        }

        public IEnumerable<DiplomaError> GetInfos()
        {
            return dataContext.DiplomaErrors
                .AsParallel()
                .Where(r => String.Equals(r.Logger, "Info", StringComparison.InvariantCultureIgnoreCase))
                .OrderByDescending(r => r.Occured)
                .ToList();
        }

        public IEnumerable<DiplomaError> GetDebugs()
        {
            return dataContext.DiplomaErrors
                .AsParallel()
                .Where(r => String.Equals(r.Logger, "Debug", StringComparison.InvariantCultureIgnoreCase))
                .OrderByDescending(r => r.Occured)
                .ToList();
        }

        public IEnumerable<DiplomaError> GetLogging()
        {
            return dataContext.DiplomaErrors
                .AsParallel()
                .Where(r => String.Equals(r.Logger, "SignIn", StringComparison.InvariantCultureIgnoreCase))
                .OrderByDescending(r => r.Occured)
                .ToList();
        }
    }
}
